package com.zdemo.leak;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestHelotPool {

	public static ThreadPoolExecutor helotPool;
	private int helotNum = 5; // 奴隶数
	private Random random = new Random(100);

	private final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();

	public TestHelotPool() {
		ThreadFactory heloFactory = new HelotFactory();
		helotPool = new ThreadPoolExecutor(helotNum, helotNum, 1000,
				TimeUnit.SECONDS, queue, heloFactory);
	}

	// 分配任务
	public void assignTask() {
		final int MAX = 100;
		final int MIN = 10;
		for (int j = 0; j < 10; j++) {
			Runnable task = new Runnable() {
//				int stoneNum = random.nextInt(MAX - MIN + 1) + MIN;// 生成10~100范围的随机数
				int stoneNum = 5;
				public void run() {
					for (int i = 1; i <= stoneNum; i++) {
						System.out.println(Thread.currentThread().getName() + " 搬完" + i + "块石头");
						if (i == 1) {
							throw new RuntimeException("搬完第1块石头不干了");
						}
						try {
							Thread.sleep(100);// 休息下
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			};
			queue.add(task);
		}
	}

	// 开始干活
	public void startWork() {
		helotPool.prestartAllCoreThreads();
	}

	public static void main(String[] args) {
		TestHelotPool pool = new TestHelotPool();
		pool.assignTask();
		pool.startWork();
	}
}
